package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LeaveApplications;
import com.ruoyi.system.service.ILeaveApplicationsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 请假申请Controller
 *
 * @author ruoyi
 * @date 2025-03-30
 */
@RestController
@RequestMapping("/system/leave")
public class LeaveApplicationsController extends BaseController
{
    @Autowired
    private ILeaveApplicationsService leaveApplicationsService;

    /**
     * 查询请假申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:leave:list')")
    @GetMapping("/list")
    public TableDataInfo list(LeaveApplications leaveApplications) {
        Long currentUserId = SecurityUtils.getUserId(); // 获取当前用户ID

        // 调用新的独立查询方法（不自动分页）
        List<LeaveApplications> list = leaveApplicationsService.selectLeaveListByRole(leaveApplications, currentUserId);
        return getDataTable(list); // 若需禁用分页，可手动返回列表
    }

    /**
     * 导出请假申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:leave:export')")
    @Log(title = "请假申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LeaveApplications leaveApplications)
    {
        List<LeaveApplications> list = leaveApplicationsService.selectLeaveApplicationsList(leaveApplications);
        ExcelUtil<LeaveApplications> util = new ExcelUtil<LeaveApplications>(LeaveApplications.class);
        util.exportExcel(response, list, "请假申请数据");
    }

    /**
     * 获取请假申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:leave:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(leaveApplicationsService.selectLeaveApplicationsById(id));
    }

    /**
     * 新增请假申请
     */
    @PreAuthorize("@ss.hasPermi('system:leave:add')")
    @Log(title = "请假申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LeaveApplications leaveApplications)
    {
        return toAjax(leaveApplicationsService.insertLeaveApplications(leaveApplications));
    }

    /**
     * 修改请假申请
     */
    @PreAuthorize("@ss.hasPermi('system:leave:edit')")
    @Log(title = "请假申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LeaveApplications leaveApplications)
    {
        return toAjax(leaveApplicationsService.updateLeaveApplications(leaveApplications));
    }

    /**
     * 删除请假申请
     */
    @PreAuthorize("@ss.hasPermi('system:leave:remove')")
    @Log(title = "请假申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(leaveApplicationsService.deleteLeaveApplicationsByIds(ids));
    }

    // 新增获取部门经理列表接口
    @PreAuthorize("@ss.hasPermi('system:leave:add')")
    @GetMapping("/approvers/{userId}")
    public AjaxResult getDeptManagers(@PathVariable Long userId) {
        return success(leaveApplicationsService.selectDeptManagersByUserId(userId));
    }

    // 新增获取本月请假天数接口
    @PreAuthorize("@ss.hasPermi('system:leave:list')")
    @GetMapping("/monthlyTotal")
    public AjaxResult getMonthlyTotal() {
        Long currentUserId = SecurityUtils.getUserId();
        int totalDays = leaveApplicationsService.selectMonthlyLeaveDays(currentUserId);
        return success(totalDays);
    }

}
